import store from '../store/index.js'
let code
let userInfo
let newCode
let werunIv
let werunEncryptedData
let werunCode
let wxdata
// 获取用户信息
function userLogin(){
	wxdata = arguments
	userInfo=uni.getStorageSync('userInfo')
	console.log(userInfo)
	if(userInfo==''){
		return new Promise((resolve, reject) => {
			let _this=this
			wxlogin()
			console.log("login执行了")
			uni.getUserProfile({
			  desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			  async success(res) {
				  console.log(res,'我执行了');
				  userInfo = res
				  console.log(res.userInfo,'用户信息')
				  store.commit('updataUserInfo',res.userInfo)
				  //uni.setStorageSync("userInfo",res.userInfo)
				  await testLogin()
				  await newInfo()
				  await resolve()
			  },
			  fail:(err)=>{
				  console.log(err)
				  resolve()
			  }
			})
			
			// uni.login({
			// 	provider: 'weixin',
			// 	success(res) {
			// 		console.log(res,'code');
			// 		code = res.code
			// 		uni.getUserInfo({
			// 			provider: 'weixin',
			// 			async success(res) {
			// 				console.log(res,'我执行了');
			// 				userInfo = res 
			// 				console.log(res.userInfo,'用户信息')
			// 				store.commit('updataUserInfo',res.userInfo)
			// 				uni.setStorageSync("userInfo",res.userInfo)
			// 				await testLogin()
			// 				await newInfo()
			// 				await login()
			// 				resolve()
			// 			},
			// 			fail(res) {
			// 				console.log(res,'授权失败')
			// 				resolve()
			// 			}
			// 		})
			// 	}
			// })
		})
	}else{
		return new Promise((resolve, reject) => {
			resolve()
		})
	}
	
}
function wxlogin(){
	new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success: function (loginRes) {
				// console.log(loginRes,'9527');
				code = loginRes.code
				newCode = loginRes.code;
				console.log(newCode,'9527');
			}
		});
	})
}
// 授权注册
async function testLogin(){
	new Promise((resolve, reject) => {
		if( wxdata[1] == 'activity' || wxdata[1] == 'video' ){
			var da = {
				code: code,
				iv: userInfo.iv,
				encryptedData: userInfo.encryptedData,
				superior_id: wxdata[0],
				invite_type: wxdata[1],
				store_id: wxdata[2],
				event_id: wxdata[3],
			}
		}else if( wxdata[0] ){
			var da = {
				code: code,
				iv: userInfo.iv,
				encryptedData: userInfo.encryptedData,
				superior_id: wxdata[0],
				store_id: wxdata[1],
			}
		}else if( !wxdata[0] ){
			var da = {
				code: code,
				iv: userInfo.iv,
				encryptedData: userInfo.encryptedData,
			}
		}
		console.log(wxdata,"??????????????????????????????????????","上下级数据");
		console.log(da,"??????????????????????????????????????","登录数据");
		uni.showLoading({
			title: '授权登录中',
		});
		let self = this
		console.log(code+"\n",userInfo.iv+"\n",userInfo.encryptedData+"\n",'data')
		console.log("code:",code)
		console.log("iv:",userInfo.iv)
		console.log("encryptedData:",userInfo.encryptedData)
		//注册
		uni.request({
			// url:store.state.URL + 'login/login/mini_register',
			data:da,
			method:"POST",
			async success(res) {
				console.log(res,'999999999999');
				await uni.hideLoading();
				await resolve()
			},
			fail(res) {
				console.log(res,'失败');
				console.log(data);
			}
		})
	})
}
// 重新获取code
function newInfo(){
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			async success (loginRes) {
				// console.log(loginRes,'9527');
				newCode = loginRes.code;
				await login()
				console.log(newCode,'9527');
				await resolve()
			}
		})
		// uni.getUserProfile({
		//   desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
		//   async success(res) {
			  
		//   },
		//   fail:(err)=>{
		// 	  console.log(err)
		// 	  resolve()
		//   }
		// });
	})
}
// 登录中
function login(){
	return new Promise((resolve, reject) => {
		console.log(newCode,"123456")
		let self = this
		uni.request({
			url:store.state.URL + 'login/login/wx_login?merchantID=1&page=',
			data:{
				code: newCode,
				merchantID:"1",
				page:""
			},
			method:"POST",
			success(res) {
				console.log(res,'授权登录成功');
				if(res.data.status) {
					// store.commit('updateToken',res.data.token);
					// uni.setStorageSync("shopID",res.store_id)
					// store.commit('updataUserId',res.data.wx_user.id);
					// 将用户的id保存在本地存储
					// uni.setStorageSync("userId",res.data.wx_user.id)
					// uni.setStorageSync("phone",res.data.wx_user.phone)
					uni.showToast({
						title: '授权登录成功！',
						icon: 'none',
						duration: 2000
					})
				}
				console.log('用户登录后的数据',res.data)
				resolve()
			}
		})
	})
}
// 检查是否已授权
function testGet(){
	console.log("wobeidiaoyongle")
	uni.getSetting({
		success(res) {
			// console.log(res)
			if(!res.authSetting['scope.userInfo']){
				newInfo()
				uni.showToast({
					title:"请先登录",
					icon:"none",
					duration:2000
				})
			}else{
				console.log('11111111111111')
			}
		},
		fail(err) {
			console.log(err)
		}
	})
}
module.exports = {
	userLogin,
	testGet,
	login
}